package com.niit.bc.biz.impl;

import com.niit.bc.bean.BookBean;
import com.niit.bc.biz.BookBiz;
import com.niit.bc.dao.BookDao;
import com.niit.bc.dao.impl.BookDaoImpl;
import com.niit.bc.util.web.PageUtil;

import javax.sql.RowSet;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookBizImpl implements BookBiz {
    BookDao bookDao = new BookDaoImpl();

    // 通过sql语句的不同查找图书的top10
    public List <BookBean> findBookList(String sql) {
        List <BookBean> list = new ArrayList <BookBean>();
        ResultSet rs = bookDao.findBookList(sql);

        BookBean bookBean = null;
        if (rs != null) {
            try {
                while (rs.next()) {
                    int i = 1;
                    bookBean = new BookBean();

                    bookBean.setBookId(rs.getInt(i++));
                    bookBean.setBookIsbn(rs.getString(i++));
                    bookBean.setBookName(rs.getString(i++));
                    bookBean.setBookPrice(rs.getDouble(i++));
                    bookBean.setBookAuthor(rs.getString(i++));
                    bookBean.setBookNum(rs.getInt(i++));
                    bookBean.setBookPub(rs.getString(i++));
                    bookBean.setBookPubTime(rs.getString(i++));
                    bookBean.setBookPhoto(rs.getString(i++));
                    bookBean.setBookDescribe(rs.getString(i++));
                    bookBean.setBookPage(rs.getInt(i++));
                    bookBean.setBookSale(rs.getInt(i++));
                    bookBean.setBookHits(rs.getInt(i++));
                    bookBean.setBookStorageTime(rs.getString(i++));
                    bookBean.setBookShowIndex(rs.getInt(i++));
                    bookBean.setBookOther(rs.getString(i++));
                    bookBean.setChildTypeId(rs.getInt(i++));

                    list.add(bookBean);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return list;
    }

    public List <BookBean> findBookShowIndex(int typeId) {
        List <BookBean> list = new ArrayList <BookBean>();
        ResultSet rs = bookDao.findBookShowIndex(typeId);

        BookBean bookBean = null;
        if (rs != null) {
            try {
                while (rs.next()) {
                    int i = 1;
                    bookBean = new BookBean();

                    bookBean.setBookId(rs.getInt(i++));
                    bookBean.setBookName(rs.getString(i++));
                    bookBean.setBookPrice(rs.getDouble(i++));
                    bookBean.setBookAuthor(rs.getString(i++));
                    bookBean.setBookNum(rs.getInt(i++));
                    bookBean.setBookPhoto(rs.getString(i++));
                    bookBean.setChildTypeId(rs.getInt(i++));
                    bookBean.setTypeId(rs.getInt(i++));

                    list.add(bookBean);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return list;
    }


    // 调用数据库访问层，获取图书详情的rowSet，并同时更新点击次数和库存量及销售量，并返回该图书的详情

    public List <BookBean> findBookByBookId(int bookId) {
        List <BookBean> bookList = new ArrayList <BookBean>();
        RowSet rs = bookDao.findBookByBookId(bookId);
        try {
            if (rs != null) {
                while (rs.next()) {
                    int row = bookDao.updateBookHits(rs.getInt(13), bookId);
                    int a = bookDao.updateBookSale(bookId);
                    if (row > 0) {
                        int i = 1;

                        // 将图书详情封装到book中

                        BookBean bookBean = new BookBean();

                        bookBean.setBookId(rs.getInt(i++));
                        bookBean.setBookIsbn(rs.getString(i++));
                        bookBean.setBookName(rs.getString(i++));
                        bookBean.setBookPrice(rs.getDouble(i++));
                        bookBean.setBookAuthor(rs.getString(i++));
                        bookBean.setBookNum(rs.getInt(i++));
                        bookBean.setBookPub(rs.getString(i++));
                        bookBean.setBookPubTime(rs.getString(i++));
                        bookBean.setBookPhoto(rs.getString(i++));
                        bookBean.setBookDescribe(rs.getString(i++));
                        bookBean.setBookPage(rs.getInt(i++));
                        bookBean.setBookSale(rs.getInt(i++));
                        bookBean.setBookHits(rs.getInt(i++) + 1);
                        bookBean.setBookStorageTime(rs.getString(i++));
                        bookBean.setBookShowIndex(rs.getInt(i++));
                        bookBean.setBookOther(rs.getString(i++));
                        bookBean.setChildTypeId(rs.getInt(i++));
                        bookBean.setChildTypeName(rs.getString(i++));
                        bookBean.setTypeId(rs.getInt(i++));
                        bookBean.setTypeName(rs.getString(i++));

                        bookList.add(bookBean);
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return bookList;
    }

    // 调用数据库访问层，获取该子类的所有图书详情的rowSet，返回该子类的所有图书详情列表

    public List <BookBean> findBookListByInfo(PageUtil pageUtil, String sql) {
        List <BookBean> list = new ArrayList <BookBean>();
        pageUtil.setRowCount(bookDao.countBooks(sql));
        RowSet rs = bookDao.findBookListByInfo(pageUtil, sql);
        if (rs != null) {
            try {
                while (rs.next()) {
                    BookBean bookBean = new BookBean();

                    int i = 1;
                    bookBean.setBookId(rs.getInt(i++));
                    bookBean.setBookName(rs.getString(i++));
                    bookBean.setBookPrice(rs.getDouble(i++));
                    bookBean.setBookAuthor(rs.getString(i++));
                    bookBean.setBookNum(rs.getInt(i++));
                    bookBean.setBookPhoto(rs.getString(i++));
                    bookBean.setBookHits(rs.getInt(i++));
                    bookBean.setBookStorageTime(rs.getString(i++));
                    bookBean.setChildTypeId(rs.getInt(i++));
                    bookBean.setBookSale(rs.getInt(i++));

                    list.add(bookBean);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    //通过关键字搜索图书列表

    public List <BookBean> findBookByKey(String key, PageUtil pageUtil) {
        // TODO Auto-generated method stub
        List <BookBean> list = new ArrayList <BookBean>();
        pageUtil.setRowCount(bookDao.countBooksByKey(key));
        RowSet rs = bookDao.findBookByKey(key, pageUtil);
        if (rs != null) {
            try {
                while (rs.next()) {
                    BookBean bookBean = new BookBean();

                    bookBean.setBookId(rs.getInt(1));
                    bookBean.setBookName(rs.getString(3));
                    bookBean.setBookPrice(rs.getDouble(4));
                    bookBean.setBookAuthor(rs.getString(5));
                    bookBean.setBookNum(rs.getInt(6));
                    bookBean.setBookPhoto(rs.getString(9));
                    bookBean.setBookHits(rs.getInt(13));
                    bookBean.setBookStorageTime(rs.getString(14));
                    bookBean.setChildTypeId(rs.getInt(17));
                    bookBean.setBookSale(rs.getInt(12));

                    list.add(bookBean);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public int addBooks(BookBean bookBean, int childTypeId) {
        // TODO Auto-generated method stub
        BookDao book = new BookDaoImpl();
        return book.addBooks(bookBean, childTypeId);
    }


}
